package org.example.service.imp;

import com.alibaba.fastjson2.JSONObject;
import org.example.dao.MessageDao;
import org.example.domain.Message;
import org.example.domain.User;
import org.example.kafka.Event;
import org.example.service.MessageService;
import org.example.util.CommonParam;
import org.example.util.HutoolUtil;
import org.example.vo.SystemMessageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MessageServiceImp implements MessageService {
    @Autowired
    private MessageDao messageDao;
    @Autowired
    private HutoolUtil hutoolUtil;
    @Autowired
    private UserServiceImp userServiceImp;
    @Override
    public int saveOne(Message message) {
        if (message == null) return 0;
        return messageDao.saveOne(message);
    }

    @Override
    public List<Message> searchTo(int toId,int start,int end) {
        if (toId <=0) return null;
        return messageDao.searchTo(toId,start,end);
    }

    @Override
    public int searchCount(int toId) {
        if (toId<=0) return 0;
        return messageDao.searchCount(toId);
    }

    @Override
    public List<Message> searchMessageByConversationId(String conversationId) {
        if (conversationId == null || hutoolUtil.stringIsEmpty(conversationId)){
            return null;
        }
        return messageDao.searchMessageByConversationId(conversationId);
    }

    @Override
    public void changeNoRead(List<Integer> ids) {
        if (ids == null || ids.size()<=0) return;
        messageDao.changeNoRead(ids);
    }

    @Override
    public int remove(int messageId) {
        if (messageId<=0) return 0;
        return messageDao.remove(messageId);
    }

    @Override
    public List<Message> searchSystemMessage(int toId, String type,int start,int limit) {
        if (toId<=0 || hutoolUtil.stringIsEmpty(type)) return null;

        return messageDao.searchSystemMessage(toId,type, start, limit);
    }

    @Override
    public int getSystemNoReadCount(int toId, String type) {
        if (toId<=0 || hutoolUtil.stringIsEmpty(type)) return 0;
        return messageDao.getSystemNoReadCount(toId,type);
    }

    @Override
    public Message changeSystemMessage(Message systemMessage) {

        if (systemMessage==null) return null;
        if (hutoolUtil.stringIsEmpty(systemMessage.getContent())) return systemMessage;
        String content = systemMessage.getContent();
        Event event = JSONObject.parseObject(content, Event.class);
        //拼接结果
        //点赞   用户xxx点赞了你的(帖子或者评论(评论有问题  点击没有处理好也不弄了))
        //评论   用户xxx评论了你的(帖子  目前就只有这个)
        //关注   用户xxx关注了你
        StringBuilder stringBuilder = new StringBuilder();
        int userId = event.getUserId();
        User user = userServiceImp.searchById(userId);
        stringBuilder.append("用户 ");
        stringBuilder.append(user.getUsername());

        if (!hutoolUtil.stringIsEmpty(event.getTopic()) && event.getTopic().equals(CommonParam.KAFKA_TOPIC_LIKE)){
            if(event.getEntityType() == 1){
                //点赞帖子
                stringBuilder.append("点赞了您的帖子");
            }else if (event.getEntityType() == 2){
                //点赞评论  这个有问题
                stringBuilder.append("点赞了您的评论");
            }
        }else if (!hutoolUtil.stringIsEmpty(event.getTopic()) && event.getTopic().equals(CommonParam.KAFKA_TOPIC_COMMENT)){
            stringBuilder.append("评论了您的帖子");
        }else if (!hutoolUtil.stringIsEmpty(event.getTopic()) && event.getTopic().equals(CommonParam.KAFKA_TOPIC_FOLLOW)){
            stringBuilder.append("关注了您");
        }
        systemMessage.setContent(stringBuilder.toString());
        return systemMessage;
    }

    @Override
    public int getSystemMessageCountByType(int toId, String type) {
        if (toId<=0 || hutoolUtil.stringIsEmpty(type)) return 0;
        return messageDao.getSystemMessageCountByType(toId,type);
    }

    /**
     * 转化一个系统消息  普通消息不处理
     * @param message
     * @return
     */
    public SystemMessageVO systemMessageToVO(Message message){
        if (message==null) return null;
        String content = message.getContent();
        if (hutoolUtil.stringIsEmpty(content)) return null;
        Event event = JSONObject.parseObject(content, Event.class);
        if (event == null) return null;
        String topic = event.getTopic();
        SystemMessageVO messageVO = new SystemMessageVO();
        if (!hutoolUtil.stringIsEmpty(topic)){
            int userId = event.getUserId();
            User user = userServiceImp.searchById(userId);
            messageVO.setWho(user);
            messageVO.setMessageTime(message.getCreateTime());
            if (CommonParam.KAFKA_TOPIC_LIKE.equals(topic)){

                if (event.getEntityType() == 1){
                    messageVO.setDoSomething("点赞了你的帖子");
                }else if (event.getEntityType() == 2){
                    messageVO.setDoSomething("点赞了你的评论");
                }
                messageVO.setJump("/discussPost/detail/"+event.getEntityId());
            }else if (CommonParam.KAFKA_TOPIC_COMMENT.equals(topic)){
                //目前评论只能评论帖子
                messageVO.setDoSomething("评论了你的帖子");
                messageVO.setJump("/discussPost/detail/"+event.getEntityId());
            }else if (CommonParam.KAFKA_TOPIC_FOLLOW.equals(topic)){
                messageVO.setDoSomething("关注了你");
                messageVO.setJump("/user/userPage.html?userId="+event.getUserId());
            }
        }

        return messageVO;
    }
    public List<SystemMessageVO> systemMessagesToVOs(List<Message> messages){
        if (messages == null || messages.size() <=0) return null;
        List<SystemMessageVO> result = new ArrayList<>();
        for (Message temp : messages){
            SystemMessageVO messageVO = systemMessageToVO(temp);
            result.add(messageVO);
        }
        return result;
    }
}
